package fi.ixonos.projects.web.usermanagement.edit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import fi.ixonos.projects.logic.bean.CodeTable;
import fi.ixonos.projects.logic.bean.Users;
import fi.ixonos.projects.logic.service.CodeTableService;
import fi.ixonos.projects.logic.service.UsersService;

public class AddNewUserWindow extends Window {

    private Users user;
    private Listbox usersListbox;

    public AddNewUserWindow() {
        this.user = new Users();
        this.user.setEnabled(Boolean.TRUE);
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public void onAddNewUser() throws Exception {
        String login = user.getUsername();
        if (login == null || login.trim().length() == 0) {
            Messagebox.show(Labels.getLabel("user.error.loginNotSpec"), Labels.getLabel("common.dialog.error"), Messagebox.OK, Messagebox.ERROR);
            return;
        }
        Textbox passwordBox = (Textbox) this.getFellow("password");
        Textbox confirmationBox = (Textbox) this.getFellow("confirmation");
        if (passwordBox.getValue().trim().length() == 0 || confirmationBox.getValue().trim().length() == 0) {
            Messagebox.show(Labels.getLabel("user.error.passNotSame"), Labels.getLabel("common.dialog.error"), Messagebox.OK, Messagebox.ERROR);
            return;
        }
        if (!passwordBox.getValue().equals(confirmationBox.getValue())) {
            Messagebox.show(Labels.getLabel("user.error.passNotSame"), Labels.getLabel("common.dialog.error"), Messagebox.OK, Messagebox.ERROR);
            return;
        }
        String password = user.getPassword();
        String name = user.getName();
        String surname = user.getSurname();
        Boolean enabled = user.getEnabled();
        Users manager = user.getManager();
        final String email = user.getEmail();
        UsersService usersService = (UsersService) SpringUtil.getApplicationContext().getBean("usersService");

        //        Listbox lb = (Listbox) this.getFellow("usersAuthorities");
//        Set selectedItems = lb.getSelectedItems();
//        Iterator selectedIterator = selectedItems.iterator();
//        List<String> authorities = new ArrayList<String>();
//        while (selectedIterator.hasNext()) {
//            Listitem li = (Listitem) selectedIterator.next();
//            CodeTable selectedAuthority = (CodeTable) li.getValue();
//            authorities.add(selectedAuthority.getCode());
//        }

        //add for each new user ROLE_USER
        List<String> authorities = new ArrayList<String>();
        authorities.add("ROLE_USER");


        try {
            if (!usersService.isUserAlreadyRegistered(login)) {
                //TODO set right user properties
                usersService.addNewUser(login, password, name, surname, email, "<phone>", "position", "location", enabled, manager, authorities);
                Messagebox.show(Labels.getLabel("user.added.successfully"), Labels.getLabel("common.dialog.information"), Messagebox.OK, Messagebox.INFORMATION);
            } else {
                Messagebox.show(Labels.getLabel("user.error.userExist"), Labels.getLabel("common.dialog.error"), Messagebox.OK, Messagebox.ERROR);
                return;
            }
        } catch (Exception e) {
            Messagebox.show(e.getMessage(), Labels.getLabel("common.dialog.error"), Messagebox.OK, Messagebox.ERROR);
            return;
        }
//        this.detach();
        // refresh listbox with users
//        List<Users> users = usersService.getAllUsers();
//        this.getUsersListbox().setModel(new BindingListModelList(users, true));
    }

    public void onAuthoritySelect(SelectEvent event) throws Exception {
        Set selectedItems = event.getSelectedItems();
        Iterator selectedIterator = selectedItems.iterator();
        List<String> selectedAuthorities = new ArrayList<String>();
        while (selectedIterator.hasNext()) {
            Listitem li = (Listitem) selectedIterator.next();
            CodeTable selectedAuthority = (CodeTable) li.getValue();
            selectedAuthorities.add(selectedAuthority.getCode());
        }
        Listbox lb = (Listbox) event.getTarget();

        StringBuilder sb = new StringBuilder();
        for (String selectedAuthority : selectedAuthorities) {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(selectedAuthority);
        }
        Bandbox bandbox = (Bandbox) getParentComponent(lb, Bandbox.class);
        bandbox.setText(sb.toString());

    }

    private Component getParentComponent(Component component, Class clazz)
            throws Exception {
        int index = 0;
        while ((component != null)
                && !clazz.isInstance((component = component.getParent()))) {
            index++;
            if (index == 50) {
                throw new Exception("No parent " + clazz + " found!");
            }
        }
        if (component == null) {
            throw new Exception("No parent " + clazz + " found!");
        }
        return component;
    }

    public void setUsersListbox(Listbox usersListbox) {
        this.usersListbox = usersListbox;
    }

    public Listbox getUsersListbox() {
        return this.usersListbox;
    }
}
